<?php
class Epic_Debug
{
/**
* Debug helper function.  This is a wrapper for var_dump() that adds
* the <pre /> tags, cleans up newlines and indents before output.
*
* @param  mixed  $var   The variable to dump.
* @param  bool   $echo  OPTIONAL Echo output if true.
* @return string
*/
    public static function dump($var, $echo = true)
    {
        // var_dump the variable into a buffer and keep the output
        ob_start();
        var_dump($var);
        $output = ob_get_clean();

        // neaten the newlines and indents
        $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
        $output = '<pre>'
                . $output
                . '</pre>';
   		if ($echo) {
            echo($output);
        }
        return $output;
    }
    
    public static function dump2($variable,$strlen=100,$width=25,$depth=10,$i=0,$objects = array())
    {
      $search = array("\0", "\a", "\b", "\f", "\n", "\r", "\t", "\v");
      $replace = array('\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v');
    
      $string = '';
    
      switch(gettype($variable)) {
        case 'boolean':      $string.= $variable?'true':'false'; break;
        case 'integer':      $string.= $variable;                break;
        case 'double':       $string.= $variable;                break;
        case 'resource':     $string.= '[resource]';             break;
        case 'NULL':         $string.= "null";                   break;
        case 'unknown type': $string.= '???';                    break;
        case 'string':
          $len = strlen($variable);
          $variable = str_replace($search,$replace,substr($variable,0,$strlen),$count);
          $variable = substr($variable,0,$strlen);
          if ($len<$strlen) $string.= '"'.$variable.'"';
          else $string.= 'string('.$len.'): "'.$variable.'"...';
          break;
        case 'array':
          $len = count($variable);
          if ($i==$depth) $string.= 'array('.$len.') {...}';
          elseif(!$len) $string.= 'array(0) {}';
          else {
            $keys = array_keys($variable);
            $spaces = str_repeat(' ',$i*2);
            $string.= "array($len)\n".$spaces.'{';
            $count=0;
            foreach($keys as $key) {
              if ($count==$width) {
                $string.= "\n".$spaces."  ...";
                break;
              }
              $string.= "\n".$spaces."  [$key] => ";
              $string.= self::dump2($variable[$key],$strlen,$width,$depth,$i+1,$objects);
              $count++;
            }
            $string.="\n".$spaces.'}';
          }
          break;
        case 'object':
          $id = array_search($variable,$objects,true);
          if ($id!==false)
            $string.=get_class($variable).'#'.($id+1).' {...}';
          else if($i==$depth)
            $string.=get_class($variable).' {...}';
          else {
            $id = array_push($objects,$variable);
            $array = (array)$variable;
            $spaces = str_repeat(' ',$i*2);
            $string.= get_class($variable)."#$id\n".$spaces.'{';
            $properties = array_keys($array);
            foreach($properties as $property) {
              $name = str_replace("\0",':',trim($property));
              $string.= "\n".$spaces."  [$name] => ";
              $string.= self::dump2($array[$property],$strlen,$width,$depth,$i+1,$objects);
            }
            $string.= "\n".$spaces.'}';
          }
          break;
      }
    
      if ($i>0) return $string;
    
      $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
      do $caller = array_shift($backtrace); while ($caller && !isset($caller['file']));
      if ($caller) $string = $caller['file'].':'.$caller['line']."\n".$string;
    
      $string = '<pre>'
      . $string
      . '</pre>';

      echo $string;
    }
}

